<template>
  <div class="cont-wrapper no-sidebar">
    <div class="block-container container">
      <div class="title">考点管理</div>
      <div class="operation">
        <el-button type="primary" class="c-btn" @click="$router.push({name: 'createSite'})">创建考点</el-button>
      </div>
      <div class="table-wrapper">
        <el-table :data="tableData" border class="c-table" style="width: 100%" v-loading="loading">
          <el-table-column label="#" width="70">
            <template slot-scope="scope">
              {{ (listQuery.page - 1) * listQuery.pageSize  + scope.$index + 1 }}
            </template>
          </el-table-column>
          <el-table-column prop="parent.areaName" label="所在区域"></el-table-column>
          <el-table-column prop="areaName" label="考点名称"></el-table-column>
          <el-table-column prop="schoolCode" label="学校代码">
            <template slot-scope="scope">
              {{ scope.row.schoolCode ? scope.row.schoolCode : '-' }}
            </template>
          </el-table-column>
          <el-table-column prop="teacherName" label="考试批数">
            <template>
              {{ form.batches }}批
            </template>
          </el-table-column>
          <el-table-column prop="teacherName" label="考试日期">
            <template>
              {{ `${form.beginDate} - ${form.endDate}` }}
            </template>
          </el-table-column>
          <el-table-column prop="teacherName" label="考试天数">
            <template>
              {{ form.days }}天
            </template>
          </el-table-column>
          <el-table-column prop="teacherName" label="操作" width="70">
            <template slot-scope="scope">
              <i class="el-icon-delete i-con" @click="handleDeleteExamSite(scope.row, scope.$index)"></i>
            </template>
          </el-table-column>
        </el-table>
        <kw-pagination v-if="tableData.length" :listQuery="listQuery" @changeCurrentPage="changeCurrentPage"></kw-pagination>
      </div>
    </div>
     <!-- 删除确认 -->
    <kw-delconfim :delVisible.sync="delDialog" delCont="确定要删除么？可以通过创建考点恢复" @handleDelete="handleDelete"></kw-delconfim>
  </div>
</template>

<script>
import kwDelconfim from '@/components/DelConfim.vue'
import kwPagination from '@/components/Pagination.vue'
import * as Utils from '@/common/util.js'
export default {
  name: 'examSiteManage',
  components: {
    kwDelconfim,
    kwPagination
  },
  data () {
    return {
      loading: false,
      tableData: [],
      rowIdx: null,
      areaName: null,
      delDialog: false,
      listQuery: {
        page: 1,
        pageSize: 10,
        total: null,
        totalPages: null
      },
      form: {
        area: '',
        batches: '',
        days: '',
        beginDate: '',
        endDate: ''
      }
    }
  },
  created () {
    this.areaName = sessionStorage.getItem('area') || ''
    this.getBatchTime()
    this.getExamSiteList()
  },
  methods: {
    // 获取考试计划时间&批次
    getBatchTime () {
      let that = this
      that.$http.findBatchTime().then(res => {
        if (res.code === 20000) {
          that.form.batches = res.data.batches
          that.form.days = res.data.days
          that.form.beginDate = Utils.getDate(res.data.beginDate, 'date')
          that.form.endDate = Utils.getDate(res.data.endDate, 'date')
        } else {
          that.$message.error(res.msg)
        }
      })
    },
    // 获取考点列表
    getExamSiteList () {
      let that = this
      that.loading = true
      let qsData = {
        areaName: this.areaName,
        page: this.listQuery.page,
        pageSize: this.listQuery.pageSize
      }
      that.$http.getExamSiteList(qsData).then(res => {
        if (res.code === 20000) {
          that.tableData = res.data.content
          that.listQuery.total = res.data.totalElements
          that.listQuery.totalPages = res.data.totalPages
        } else {
          that.$message.error(res.msg)
        }
        that.loading = false
      })
    },
    // 删除指定考点
    handleDeleteExamSite (row, idx) {
      this.rowIdx = idx
      this.areaName = row.areaName
      this.delDialog = true
    },
    // 删除回调 单个
    handleDelete () {
      let that = this
      that.$http.delExamSite({ areaName: that.areaName }).then(res => {
        if (res.code === 20000) {
          that.tableData.splice(that.rowIdx, 1)
          that.$message.success('删除考点成功')
          that.listQuery.total--
        } else {
          that.$message.error(res.msg)
        }
      })
    },
    // 翻页
    changeCurrentPage (queryData) {
      this.listQuery.page = queryData.page
      this.listQuery.pageSize = queryData.pageSize
      this.getExamSiteList()
    }
  }
}
</script>

<style>
.container {
  margin-left: 0;
  width: 100%;
}
</style>
